package com.jy.servlet;

import com.jy.dao.ManageDao;
import com.jy.dao.UserDao;
import com.jy.dao.impl.ManageDaoImpl;
import com.jy.dao.impl.UserDaoImpl;
import com.jy.entity.*;
import com.jy.util.JSONTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

@WebServlet("/TestlogServlet/*")
public class TestlogServlet extends BaseServlet {
    private int hu=1;
    private  int lis=20;

    ManageDao us=new ManageDaoImpl();

    /**
     * 分页查询所有
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void seall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        try {
            int h=us.logcount();
            if(request.getParameter("page")!=null && request.getParameter("page")!="") {
                hu=Integer.valueOf(request.getParameter("page"));
            }
            //得到每页的数据
            if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
                lis=Integer.valueOf(request.getParameter("limit"));
            }
            int xd=(hu-1)*lis;
            if(lis!=-1) {
                List list=us.findsell(xd,lis);
                json= JSONTool.jsontool(list);
                String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+h+",\"data\":"+json+"}";
                response.getWriter().write(heu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    /**
     * 删除记录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    protected void de(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String id=request.getParameter("id");
        int exroid=Integer.parseInt(id);
        int d=us.detestlog(exroid);
        if(d>0){
            json="desug";
            response.getWriter().write(json);
        }else{
            json="delo";
            response.getWriter().write(json);
        }
    }

    /**
     * 搜索
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    protected void serach(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String sea=request.getParameter("seaname");
        int coun=us.seacount(sea);
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            List list=us.search(xd,lis,sea);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }

    /**
     * 分页查询试卷
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getexampage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        try {
            int h=us.getstucount("select count(*) from exam");
            if(request.getParameter("page")!=null && request.getParameter("page")!="") {
                hu=Integer.valueOf(request.getParameter("page"));
            }
            //得到每页的数据
            if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
                lis=Integer.valueOf(request.getParameter("limit"));
            }
            int xd=(hu-1)*lis;
            if(lis!=-1) {
                List list=us.getexamcenter(xd,lis);
                json= JSONTool.jsontool(list);
                String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+h+",\"data\":"+json+"}";
                response.getWriter().write(heu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
    //模糊查询试卷
    protected void findexam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String sea=request.getParameter("seaname");
        int coun=us.getstucount("select count(*) from exam where exam_name like '%"+sea+"%'");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            List list=us.findexam_name(xd,lis,sea);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }

    //分类查询排行榜
    protected void examtype(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String sea1=request.getParameter("seaname");
        int sea=Integer.parseInt(sea1);
        System.out.println("试卷id:"+sea);
        int coun=us.getstucount("SELECT count(*) FROM exam_record e,student s,classes c WHERE e.user_id=s.stid AND e.exam_id=? AND s.classId=c.classId AND e.exam_zt='已交卷'",sea);
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT s.stname,c.className,e.exam_score,e.exam_name,e.exam_time FROM exam_record e,student s,classes c WHERE e.user_id=s.stid AND e.exam_id=? AND s.classId=c.classId AND e.exam_zt='已交卷'  ORDER BY e.exam_score DESC limit ?,?";
            List list=us.orderbyscore(sql,sea,xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }
    //分页查询错题排行榜
    protected void falsetop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        int coun=us.getstucount("SELECT COUNT(*) FROM (SELECT q.question_id,q.question_name,f.question_answera,f.question_answerb,f.question_answerc,f.question_answerd,f.question_answer,f.exam_type,COUNT(*) AS COUNT FROM falsebook f,question q WHERE f.question_id=q.question_id GROUP BY f.question_id ORDER BY COUNT DESC\n" +
                ") AS falseb");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT q.question_id,q.question_name,f.question_answera,f.question_answerb,f.question_answerc,f.question_answerd,f.question_answer,f.exam_type,COUNT(*) AS COUNT FROM falsebook f,question q WHERE f.question_id=q.question_id GROUP BY f.question_id ORDER BY COUNT DESC LIMIT ?,?";
            List list=us.orderbyfalse(sql,xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }
    //分页查询教学视频
    protected void stuvideo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        int coun=us.getstucount("select count(*) from video where vtype=?","教学视频");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT v.vid,v.vname,v.vdesc,v.vtype,v.vurl,t.tname,v.vtime FROM video v,teacher t WHERE v.teacherid=t.teacherId AND v.vtype=? limit ?,?";
            List list=us.getstuvideo(sql,"教学视频",xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }
    //分页查询课堂回放
    protected void clsvideo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        int coun=us.getstucount("select count(*) from video where vtype=?","课堂回放");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT v.vid,v.vname,v.vdesc,v.vtype,v.vurl,c.className,t.tname,v.vtime FROM video v,teacher t,classes c WHERE v.teacherid=t.teacherId AND c.classId=v.classid AND v.vtype=? limit ?,?";
            List list=us.getclsvideo(sql,"课堂回放",xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }
    //模糊查询教学视频
    protected void findstuvideo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String sea=request.getParameter("seaname");
        System.out.println(sea);
        int coun=us.getstucount("select count(*) from video where vname like '%"+sea+"%' and vtype=?","教学视频");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT v.vid,v.vname,v.vdesc,v.vtype,v.vurl,t.tname,v.vtime FROM video v,teacher t WHERE v.teacherid=t.teacherId and v.vname like '%"+sea+"%' AND v.vtype=? limit ?,?";
            List list=us.getstuvideo(sql,"教学视频",xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }
    //模糊查询课堂回放
    protected void findclsvideo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String sea=request.getParameter("seaname");
        int coun=us.getstucount("select count(*) from video where vname like '%"+sea+"%' and vtype=?","课堂回放");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT v.vid,v.vname,v.vdesc,v.vtype,v.vurl,c.className,t.tname,v.vtime FROM video v,teacher t,classes c WHERE v.vname like '%"+sea+"%' and v.teacherid=t.teacherId AND c.classId=v.classid AND v.vtype=? limit ?,?";
            List list=us.getclsvideo(sql,"课堂回放",xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }
    //删除视频
    protected void delvideo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String id=request.getParameter("id");
        int exroid=Integer.parseInt(id);
        int d=us.delvideo(exroid);
        if(d>0){
            json="desug";
            response.getWriter().write(json);
        }else{
            json="delo";
            response.getWriter().write(json);
        }
    }
    //删除试卷
    protected void delexam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String id=request.getParameter("id");
        int exroid=Integer.parseInt(id);
        int d=us.delexam(exroid);
        int n=us.updaterecord(exroid);
        if(n>0){
            System.out.println("试卷状态修改成功...");
        }else{
            System.out.println("试卷状态修改失败...");
        }
        if(d>0){
            json="desug";
            response.getWriter().write(json);
        }else{
            json="delo";
            response.getWriter().write(json);
        }
    }

    /**
     * 分页根据id查成绩排行榜
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void scoretop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        try {
            int h=us.getstucount("SELECT count(*) FROM exam_record e,student s,classes c WHERE e.user_id=s.stid AND s.classId=c.classId AND e.exam_zt='已交卷'");
            if(request.getParameter("page")!=null && request.getParameter("page")!=""){
                hu=Integer.valueOf(request.getParameter("page"));
            }
            //得到每页的数据
            if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
                lis=Integer.valueOf(request.getParameter("limit"));
            }
            int xd=(hu-1)*lis;
            if(lis!=-1) {
                List list=us.orderbyscore("SELECT s.stname,c.className,e.exam_score,e.exam_name,e.exam_time FROM exam_record e,student s,classes c WHERE e.user_id=s.stid AND s.classId=c.classId AND e.exam_zt='已交卷'  ORDER BY e.exam_score DESC limit ?,?",xd,lis);
                json= JSONTool.jsontool(list);
                String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+h+",\"data\":"+json+"}";
                response.getWriter().write(heu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    //分页查询试题中心
    protected void getquestionspage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        try {
            int h=us.getstucount("select count(*) from question");
            if(request.getParameter("page")!=null && request.getParameter("page")!="") {
                hu=Integer.valueOf(request.getParameter("page"));
            }
            //得到每页的数据
            if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
                lis=Integer.valueOf(request.getParameter("limit"));
            }
            int xd=(hu-1)*lis;
            if(lis!=-1) {
                String sql="SELECT q.question_id,q.question_name,q.question_score,a.aname,q.question_type,q.question_optiona,q.question_optionb,q.question_optionc,q.question_optiond,q.question_answer,q.update_time,b.book_name FROM question q,admin a,book b WHERE  q.question_creator=a.id AND q.question_bookid=b.book_id limit ?,?";
                List list=us.findquestionspage(sql,xd,lis);
                json= JSONTool.jsontool(list);
                String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+h+",\"data\":"+json+"}";
                response.getWriter().write(heu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
    //模糊查询题目
    protected void findquestions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String sea=request.getParameter("seaname");
        int coun=us.getstucount("select count(*) from question where question_name like '%"+sea+"%'");
        if(request.getParameter("page")!=null && request.getParameter("page")!="") {
            hu=Integer.valueOf(request.getParameter("page"));
        }
        //得到每页的数据
        if(request.getParameter("limit")!=null && request.getParameter("limit")!="") {
            lis=Integer.valueOf(request.getParameter("limit"));
        }
        int xd=(hu-1)*lis;
        if(lis!=-1){
            String sql="SELECT q.question_id,q.question_name,q.question_score,a.aname,q.question_type,q.question_optiona,q.question_optionb,q.question_optionc,q.question_optiond,q.question_answer,q.update_time,b.book_name FROM question q,admin a,book b WHERE q.question_name like '%"+sea+"%' and q.question_creator=a.id AND q.question_bookid=b.book_id limit ?,?";
           List list=us.findquestionspage(sql,xd,lis);
            json= JSONTool.jsontool(list);
            String heu="{\"code\":\"0\",\"msg\":\"\",\"count\":"+coun+",\"data\":"+json+"}";
            response.getWriter().write(heu);
        }

    }

    protected void delquestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        response.setContentType("text/html; charset=UTF-8");
        String json="";
        String id=request.getParameter("id");
        int exroid=Integer.parseInt(id);
        int d=us.delquestion(exroid);
        if(d>0){
            json="desug";
            response.getWriter().write(json);
        }else{
            json="delo";
            response.getWriter().write(json);
        }
    }
    //修改题目
    protected void upquestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException {
        String json="";
        String name=request.getParameter("name");
        String desc=request.getParameter("desc");
        int score=Integer.parseInt(desc);
        String limi=request.getParameter("lim");
        String bookid=request.getParameter("bookid");
        String id=request.getParameter("id");
        int questionid=Integer.parseInt(id);
        UserDao ud=new UserDaoImpl();
        book b=ud.getbook2("select *from book where book_name=?",bookid);
        Question q=new Question();
        q.setQuestion_name(name);
        q.setQuestion_score(score);
        q.setQuestion_answer(limi);
        q.setQuestion_bookid(b.getBook_id());
        q.setQuestion_id(questionid);
        int upex=us.upquestion(q);
        if(upex>0){
            json="upasug";
            response.getWriter().write(json);
        }else{
            json="uploa";
            response.getWriter().write(json);
        }
    }

}
